package gao.xiaolei.controller;

import gao.xiaolei.dto.*;
import gao.xiaolei.entity.User;
import gao.xiaolei.exception.AuthException;
import gao.xiaolei.service.AuthService;
import gao.xiaolei.service.ClassesService;
import gao.xiaolei.service.GroupService;
import gao.xiaolei.util.JwtUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import gao.xiaolei.entity.Groupcs;
import gao.xiaolei.service.UserService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {//这个controller是用来其他内部服务用的

    @Resource(name = "AuthService")
    private AuthService authService;

    @Resource(name = "UserService")
    private UserService userService;

    @Resource(name = "ClassesService")
    private ClassesService classesService;

    @Resource(name = "GroupService")
    private GroupService groupService;

    @GetMapping("/realName/{id}")
    public String getRealNameById(@PathVariable("id") String id) {
        return userService.getRealName(id);
    }

    @GetMapping("/realName")
    public UserRealNameDto getRealName(@CookieValue("access_token") String access_token) {
        String userId = JwtUtil.getUserId(access_token);
        return new UserRealNameDto(userId, authService.getRealName(userId));
    }

    @GetMapping("/group/leader/{userId}")
    public List<Groupcs> getInWitchGroupIsLeader(@PathVariable("userId") String userId) {
        return userService.getInWitchGroupIsLeader(userId);
    }

    @GetMapping("/groupLeader/{groupId}")
    public String getGroupLeader(@PathVariable("groupId") String groupId) {
        return userService.getGroupLeader(groupId);
    }

    @GetMapping("/groupLeadingCadre/{groupId}")
    public String getGroupLeadingCadre(@PathVariable("groupId") String groupId) {
        return userService.getGroupLeadingCadre(groupId);
    }

    @GetMapping("/groupName/{id}")
    public String getGroupName(@PathVariable(name = "id") String groupId) {
        return userService.getGroupName(groupId);
    }

    @GetMapping("/groupMember/{id}")
    public List<String> getGroupMember(@PathVariable(name = "id") String groupId) {
        return userService.getGroupMembers(groupId);
    }

    @GetMapping("/classesStore/{currentUserId}")
    //用户获取所有能够加入的班级列表
    public List<ClassesStoreListDto> getClassesStoreList(@PathVariable("currentUserId") String currentUserId) {
        return classesService.getClassesStoreList(currentUserId);
    }

    @PostMapping("/classes")
    //用户报名班级
    public Integer joinClasses(@RequestParam("currentUserId") String currentUserId,
                               @RequestParam("classesId") String classesId) {
        return classesService.joinClasses(currentUserId, classesId);
    }

    @PutMapping("/classes")
    //用户退出班级
    public Integer exitClasses(@RequestParam("currentUserId") String currentUserId,
                               @RequestParam("classesId") String classesId) {
        return classesService.exitClasses(currentUserId, classesId);
    }

    @GetMapping("/basic/{currentUserId}")
    //获取用户基本信息
    public UserBaseDetail getUserBaseDetail(@PathVariable("currentUserId") String currentUserId) {
        return userService.getUserBaseDetail(currentUserId);
    }

    @PutMapping("/pwd")
    //修改用户密码
    public void updatePassword(@RequestParam("userId") String userId,
                               @RequestParam("oldPassword") String oldPassword,
                               @RequestParam("newPassword") String newPassword) {
        userService.updatePassword(userId, oldPassword, newPassword);
    }

    @PutMapping("/basic")
    //修改基本信息
    public void updateBasicMessage(@RequestBody User user) {
        userService.updateUserBaseDetail(user);
    }

    @GetMapping("/inWitchClasses/{userId}")
    //获取用户所在在班级列表(树形结构)
    public List<TreeDto> getUserInWitchClasses(@PathVariable("userId") String userId) {
        return classesService.getRootClassesByUserIn(userId);
    }

    @GetMapping("/inWitchGroupInClasses/{classesId}/{userId}")
    //获取用户所在在班级的组列表(树形结构)
    public List<TreeDto> getUserGroupInWitchClasses(@PathVariable("classesId") String classesId,
                                                    @PathVariable("userId") String userId) {
        return groupService.getClassesGroupByUserIn(classesId, userId);
    }

    //获取所有用户列表（除超级管理员）
    @GetMapping("/allUser")
    public List<User> getAllUserList() {
        return userService.getAllUser();
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AuthException.class)
            code = 401;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }

}
